package com.xywl.escort.view.splash;

import com.xywl.escort.bean.BannerReq;
import com.xywl.escort.bean.BannerResp;
import com.xywl.escort.net.BaseSubscriber;
import com.xywl.escort.net.ExceptionHandle;
import com.xywl.escort.net.api.service.ApiServer;
import com.xywl.escort.utils.RxUtil;

import javax.inject.Inject;

/**
 * Created by 32422 on 2017/10/25.
 */

public class SplashPresenter implements SplashContract.Presenter {
    private SplashContract.View mView;
    private ApiServer mApiServer;

    @Inject
    SplashPresenter(ApiServer apiServer) {
        mApiServer = apiServer;
    }

    @Override
    public void subscribe() {

    }

    @Override
    public void unSubscribe() {
        mView = null;
    }

    @Override
    public void takeView(SplashContract.View view) {
        this.mView = view;
    }

    @Override
    public void getAdvert() {
        BannerReq req = new BannerReq();
        req.position = 0;
        mApiServer.focusPicture(req)
                .compose(RxUtil.<BannerResp>applySchedulers(RxUtil.IO_ON_UI_TRANSFORMER_BACK_PRESSURE))
                .compose(mView.<BannerResp>bindToLife())
                .subscribe(new BaseSubscriber<BannerResp>() {
                    @Override
                    protected void hideDialog() {

                    }

                    @Override
                    protected void showDialog() {

                    }

                    @Override
                    public void onError(ExceptionHandle.ResponseThrowable e) {
                        mView.getAdvertFailure();
                    }

                    @Override
                    public void onNext(BannerResp resp) {
                        if (null != resp && resp.isOk()) {
                            if (null != resp.data && resp.data.size() > 0) {
                                BannerResp.DataBean dataBean = resp.data.get(0);
                                mView.getAdvertSuccess(dataBean);
                            }
                        } else {
                            mView.getAdvertFailure();
                        }
                    }
                });
    }
}
